package com.ccsc.auth.service.impl;

import com.ccsc.auth.dao.PositionDao;
import com.ccsc.auth.entity.Department;
import com.ccsc.auth.entity.Position;
import com.ccsc.auth.service.PositionService;
import com.ccsc.auth.utils.DataMap;
import com.ccsc.auth.utils.Page;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 职位position
 * Created by yyangs on 2017/2/14.
 */
@Service
public class PositionServiceImpl implements PositionService {
    @Autowired
    private PositionDao positionDao;

    @Override
    public List<Position> getAllPositions() throws Exception {
        return positionDao.getAllPositions();
    }

    @Override
    public List<Map<Department, Position>> getDepartmentAndPositionsByUserId(Integer userId) throws Exception {
        List<Map<Department, Position>> mapList=new ArrayList<>();
        List<Position> positions=positionDao.getPositionsByUserId(userId);
        for (Position position:positions) {
            Map<Department,Position> departmentPositionMap=new HashMap<>();
            departmentPositionMap.put(position.getDepartment(),position);
            mapList.add(departmentPositionMap);
            mapList.add(departmentPositionMap);
        }
        return mapList;
    }

    @Override
    public List<Position> getPositionsByStatus(Integer Status) throws Exception {
        Position position=new Position();
        position.setStatus(Status);
        return positionDao.getPositionInfosByCondition(position);
    }

    @Override
    public List<Position> getPositionsByName(String name) throws Exception {
        Position position=new Position();
        position.setName(name);
        return positionDao.getPositionInfosByCondition(position);
    }

    @Override
    public List<Position> getPositionsByDepartmentId(Integer depertmentId) throws Exception {
        return positionDao.getPositionsByDepartmentId(depertmentId);
    }

    @Override
    public List<Position> getPositionsByParentPositionId(Integer parentPositionId) throws Exception {
        Position position=new Position();
        Position parentPosition=new Position();
        parentPosition.setId(parentPositionId);
        position.setParentPosition(parentPosition);
        return positionDao.getPositionInfosByCondition(position);
    }

    @Override
    public Position getDepartmentAndPosition(Position position) throws Exception {
        return positionDao.getDepartmentAndPosition(position);
    }

    @Override
    public Page getPositionsByPage(DataMap params) throws Exception {
        List<Position> resultList = positionDao.getAllPositions( new PageBounds(params.getInt("page"), params.getInt("pageSize")));
        PageList<Position> pageList = (PageList<Position>) resultList;
        return new Page(pageList);
    }

    @Override
    public Page getPositionsByPage(Position position, DataMap params) throws Exception {
        List<Position> resultList = positionDao.getPositionInfosByCondition(position, new PageBounds(params.getInt("page"), params.getInt("pageSize")));
        PageList<Position> pageList = (PageList<Position>) resultList;
        return new Page(pageList);
    }

    @Override
    public Position getPositionById(Integer id) throws Exception {
        Position position=new Position();
        position.setId(id);
        return positionDao.getPositionInfoByCondition(position);
    }

    @Override
    public Integer createPosition(Position position) throws Exception {
        return positionDao.createPosition(position);
    }

    @Override
    public Integer updatePosition(Position position) throws Exception {
        return positionDao.updatePosition(position);
    }

    @Override
    public Integer deletePositionById(Integer id) throws Exception {
        //先要将部门职位该id置为空
        Position position=new Position();
        position.setId(id);
        return positionDao.deletePosition(position);
    }

    @Override
    public Integer createDepartmentAndPosition(Position position) throws Exception {
        return positionDao.createDepartmentAndPosition(position);
    }

    @Override
    public Integer updateDepartmentAndPosition(Position position) throws Exception {
        return positionDao.updateDepartmentAndPosition(position);
    }

    @Override
    public Integer deleteDepartmentAndPosition(Integer departmentId,Integer positionId) throws Exception {
        Department department=new Department();
        department.setId(departmentId);
        Position position=new Position();
        position.setId(positionId);
        DataMap dataMap=new DataMap();
        dataMap.put("department",department);
        dataMap.put("position",position);
        return positionDao.deleteDepartmentAndPosition(dataMap);
    }

    @Override
    public Integer activePosition(Integer id) throws Exception {
        //状态改为1，表示激活
        Position position=new Position();
        position.setId(id);
        position.setStatus(1);
        return positionDao.updatePosition(position);
    }

    @Override
    public Integer withdrawPosition(Integer id) throws Exception {
        //状态改为0，表示注销
        Position position=new Position();
        position.setId(id);
        position.setStatus(0);
        return positionDao.updatePosition(position);
    }

    @Override
    public Integer batchActivePosition(List<Integer> ids) throws Exception {
        int result=0;
        for (Integer id:ids) {
            result=activePosition(id);
        }
        return result;
    }

    @Override
    public Integer batchWithdrawPosition(List<Integer> ids) throws Exception {
        int result=0;
        for (Integer id:ids) {
            result=withdrawPosition(id);
        }
        return result;
    }
}
